package com.handinfo.android.ui.window;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.object.Scene;
import com.handinfo.android.core.object.Scene.SceneHandler;
import com.handinfo.android.game.GameActor;
import com.handinfo.android.game.GameActorConstants;
import com.handinfo.android.game.IconManger;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWListSubItem;
import com.handinfo.android.uicontrols.controls.DWListbox;
import com.handinfo.android.uicontrols.controls.DWPopMenu;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UINrearby implements IUIWindows{

	private boolean flag = false;
	private int menu_index = 0;
	private GameActor m_near_gameActor = null;
	private DWFrame m_near_frame = null;
	private DWTitle m_near_title = null;
	private DWImageBox m_imageBox_title = null;
	private Bitmap m_bm_title = null;
	private DWButton m_button_refresh = null;
	private Bitmap m_bm_refresh01 = null;
	private Bitmap m_bm_refresh02 = null;
	private DWBackground m_near_backGround = null;
	private DWListbox m_near_listBox = null;
	private DWImageBox m_imageBox_close = null;
	private Bitmap m_bm_close = null;
	private DWPopMenu m_near_menu = null;
	private String[] menus_haoyou = {"密语","交易","查看","加为好友","添加黑名单","邀请组队"};
	private String[] menus_haoyou01 = {"密语","交易","查看","加为好友","添加黑名单","邀请组队","收徒"};
	private String[] menus_haoyou02 = {"密语","交易","查看","加为好友","添加黑名单","邀请组队","拜师"};	
	private Bitmap m_bm_anniu = null;
	private Bitmap m_bm_anxia = null;
	
	private void nearbyPeople(){
		if(DWGameManager.AdpaterType == 2){
			m_near_frame = new DWFrame(DWGameManager.Screen_Width*18/80	, DWGameManager.Screen_Height*28/48);
		}else{
			m_near_frame = new DWFrame(180, 280);
		}

		m_near_frame.setThrough(true);
		m_near_frame.setClickClose(true);
		m_near_frame.setNearAnchor(UIMain.m_Uimain_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 0, DWGameManager.Screen_Height*10/48);
		m_near_title = new DWTitle("", m_near_frame);
		
		m_imageBox_title = new DWImageBox(m_bm_title);
		m_imageBox_title.setNearAnchor(m_near_title, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		
		m_imageBox_close = new DWImageBox(m_bm_close);
		m_imageBox_close.addListener(m_close);
		m_imageBox_close.setNearAnchor(m_near_title, Tools.RIGHT_TOP, Tools.RIGHT_TOP, 0, 0);
		
		m_near_backGround = new DWBackground(160, 220);
		if(DWGameManager.AdpaterType == 2){
			m_near_backGround.setShowWideHigh(DWGameManager.Screen_Width*16/80	, DWGameManager.Screen_Height*22/48);
		}
		m_near_backGround.setNearAnchor(m_near_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		
		if(DWGameManager.AdpaterType == 2){
			m_near_listBox = new DWListbox(DWGameManager.Screen_Width*15/80	, DWGameManager.Screen_Height*20/48);
		}else{
			m_near_listBox = new DWListbox(150, 200);
		}
		m_near_listBox.addListener(m_lostBox);
		m_near_listBox.setNearAnchor(m_near_backGround, Tools.HCENTER_TOP, Tools.HCENTER_TOP, 0, 10);
		reFreshInfosData();
		
		m_button_refresh = new DWButton("刷新", m_bm_refresh01);
		m_button_refresh.setDownImage(m_bm_refresh02);
		m_button_refresh.addListener(m_refresh);
		m_button_refresh.setNearAnchor(m_near_frame, Tools.HCENTER_BOTTOM, Tools.HCENTER_BOTTOM, 0, 0);
		
		m_near_frame.addControl(m_near_backGround);
		m_near_frame.addControl(m_near_title);
		m_near_frame.addControl(m_imageBox_title);
		m_near_frame.addControl(m_imageBox_close);
		m_near_frame.addControl(m_near_listBox);
		m_near_frame.addControl(m_button_refresh);
		DWControlsManager.getInstance().addControl(m_near_frame);
		
	}
	
	/** 打开或刷新列表 */
	public void reFreshInfosData(){
		m_near_listBox.removeAllSubItem();
		Scene sence = ((SceneHandler) DWGameManager.getInstance().getGameHandler()).m_scene;
		if(sence != null){
			Object[] actors = sence.m_drawList.toArray();
			if(actors != null && actors.length > 0){
				GameActor ga;
				for(int i=0;i<actors.length;i++){
					if(actors[i] instanceof GameActor){
					    ga = (GameActor)actors[i];
					    if(ga.m_type == GameActorConstants.ACTOR_TYPE_ROLE && sence.m_controlledActor.m_gameId != ga.m_gameId){
					    	DWListSubItem subitem = new DWListSubItem();
					    	subitem.setName(String.valueOf(ga.m_gameId));
					    	DWTextbox textbox;
					    	if(DWGameManager.AdpaterType == 2){
					    		textbox = new DWTextbox(getShowWordColor("FFFFFF", ga.m_name),DWGameManager.Screen_Width/8);
						    	textbox.setFont(DWFont.getFont(24));
					    	}else{
					    		textbox = new DWTextbox(getShowWordColor("FFFFFF", ga.m_name),100);
						    	textbox.setFont(DWFont.getFont(18));
					    	}
					    	DWTextbox levelbox;
					    	if(DWGameManager.AdpaterType == 2){
					    		levelbox = new DWTextbox(String.valueOf(ga.m_lv),DWGameManager.Screen_Width*3/80);
					    	}else{
					    		levelbox = new DWTextbox(String.valueOf(ga.m_lv),30);
					    	}
                            
                            Bitmap bitmap = null;
                            if(ga.m_faction > 0){                            	
                            	bitmap = IconManger.m_factionIcon[ga.m_faction - 1];
                            }
                            
                            
                            textbox.setNearAnchor(subitem, Tools.LEFT_TOP, Tools.LEFT_TOP, 0, 0);
                            levelbox.setNearAnchor(textbox, Tools.LEFT_TOP, Tools.RIGHT_TOP, 0, 0);
                            if(bitmap != null){
                            	DWImageBox factionbox = new DWImageBox(bitmap);
                                factionbox.setNearAnchor(levelbox, Tools.LEFT_TOP, Tools.RIGHT_TOP, 0, 0);
                                subitem.addControls(factionbox);
                            }
                            
                            subitem.addControls(textbox);
                            subitem.addControls(levelbox);
                            
                            m_near_listBox.addSubItem(subitem);
					    }
					}
				}
				
			}
		}
	}
	
	
   /** 设置列表内容的字的颜色 */
	public String getShowWordColor(String color,String text){
		String temp = null;
		temp = "<mark c="+color+">"+text+"</mark>";
		return temp;
	}
	
	//关闭窗口
	private DWListener m_close = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			UIWindows.getInstance().m_nearby.close((byte)0);
		}
	};
	//list菜单监听
	private DWListener m_lostBox = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			int index = m_near_listBox.getTouchIndex();
//			menu_index = index;
            DWListSubItem subItem = m_near_listBox.getListSubItem(index);
            long gameID = Tools.getLong(subItem.getName());
            Scene sence = ((SceneHandler) DWGameManager.getInstance().getGameHandler()).m_scene;
            Object[] actors = sence.m_drawList.toArray();
            GameActor ga = null;
            for(Object actor:actors){
            	if(actor instanceof GameActor){
            		GameActor ta = (GameActor)actor;
            		if(ta.m_gameId == gameID){
            			ga = ta;
            			break;
            		}
            	}
            }
            if(ga == null){//目标不在显示范围了
            }else{
            	if(ga.m_faction == sence.m_controlledActor.m_faction ){//目标存在并且和自己同一阵营
            		m_near_gameActor = ga;
            		if (m_near_gameActor.m_lv <= DWGameManager.getInstance().m_role.m_shitu_tudilv) {// 目标等级是徒弟
        				if (DWGameManager.getInstance().m_role.m_lv >= DWGameManager.getInstance().m_role.m_shitu_laoshi) {// 自己是师父
        					m_near_menu = new DWPopMenu(menus_haoyou01,m_bm_anniu);
        				} else {
        					m_near_menu = new DWPopMenu(menus_haoyou,m_bm_anniu);
        				}
        			}else if(m_near_gameActor.m_lv >= DWGameManager.getInstance().m_role.m_shitu_laoshi){//目标是老师
        				if(DWGameManager.getInstance().m_role.m_lv <= DWGameManager.getInstance().m_role.m_shitu_tudilv){
        					m_near_menu = new DWPopMenu(menus_haoyou02, m_bm_anniu);
        				}else{
        					m_near_menu = new DWPopMenu(menus_haoyou, m_bm_anniu);
        				}
        				
        			}else{
        				m_near_menu = new DWPopMenu(menus_haoyou, m_bm_anniu);
        			}			
            		
            		m_near_menu.setNearAnchor(m_near_frame, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, -50);
            		m_near_menu.setDownImage(m_bm_anxia);
            		m_near_menu.addListener(m_near);
    				m_near_frame.addControl(m_near_menu);
            		
            	}
            	sence.setTargetActor(ga);
            }
		}
	};
	
	//刷新按钮
	private DWListener m_refresh = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			reFreshInfosData();
		}
	};

	//pop菜单监听
	public boolean m_near_flag = true;
	public long m_near_id = 0;
	private DWListener m_near = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			int index = m_near_menu.getIndex();
			Tools.debugPrintln("===========Tools=================="+index);
			switch(index){
			case 0://密语
				    m_near_flag = false;
				    m_near_id = m_near_gameActor.m_gameId;
					UIWindows.getInstance().m_checked.ChatPrivate(m_near_gameActor.m_gameId,m_near_gameActor.m_name);
					break;
				case 1://交易
					UIWindows.getInstance().m_exchang.setActor(m_near_gameActor.m_gameId,m_near_gameActor.m_name);
					UIWindows.getInstance().m_exchang.open((byte)0);
					break;
				case 2://查看
					DWGameManager.getInstance().getSendMessage().sendSeeOtherRoleInfo(m_near_gameActor.m_gameId);
					break;
				case 3://添加好友			
					DWGameManager.getInstance().getSendMessage().sendSocialAddForName(UISocial.SOCIAL_TYPE_FRIEND, m_near_gameActor.m_name);
					break;
				case 4://添加黑名单
					DWGameManager.getInstance().getSendMessage().sendSocialAddForName(UISocial.SOCIAL_TYPE_BLACKLIST, m_near_gameActor.m_name);
					break;
				case 5://邀请组队
					DWGameManager.getInstance().getSendMessage().sendTeamRequest(m_near_gameActor.m_gameId);				
					break;
				case 6://拜师
					DWGameManager.getInstance().getSendMessage().sendBaiShi(m_near_gameActor.m_gameId);
					break;
			}
			m_near_frame.removeControl(m_near_menu);
			
		}
	};
	
	public void init() {
		m_bm_close = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		m_bm_anniu = UIWindows.createImage("/img/newui/anniu_10.gnp");
		m_bm_anxia = UIWindows.createImage("/img/newui/anniu_10ax.gnp");
		m_bm_title = UIWindows.createImage("/img/newui/zhouweiwanjiabt.gnp");
		m_bm_refresh01 = UIWindows.createImage("/img/newui/anniu_2.gnp");
		m_bm_refresh02 = UIWindows.createImage("/img/newui/anniu_2ax.gnp");
	}

	public void open(byte index) {
		if(flag==false){
			flag = true;
			nearbyPeople();
		}else{
			if(m_near_frame != null){
				m_near_frame = null;
				flag = true;
				nearbyPeople();
			}else{
				return;
			}
		}
	}

	public void close(byte index) {
		flag = false;
		DWControlsManager.getInstance().removeControl(m_near_frame);
	}

	public void dispose() {
	}

	public void logic() {
		// TODO Auto-generated method stub
		
	}

}
